package cn.stylefeng.guns.modular.addressbook.service.impl;

import cn.stylefeng.guns.core.consts.ProjectConstants;
import cn.stylefeng.guns.modular.addressbook.entity.Department;
import cn.stylefeng.guns.modular.addressbook.mapper.TreeDepartmentMapper;
import cn.stylefeng.guns.modular.addressbook.pojo.response.TreeDepartmentTypeInfo;
import cn.stylefeng.guns.modular.addressbook.service.DepartmentService;
import cn.stylefeng.guns.modular.addressbook.service.TreeDepartmentService;
import cn.stylefeng.guns.modular.emergency.entity.RelationLevel;
import cn.stylefeng.roses.kernel.rule.tree.factory.DefaultTreeBuildFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ┌─┐       ┌─┐ + +
 * ┌──┘ ┴───────┘ ┴──┐++
 * │                 │
 * │       ───       │++ + + +
 * ███████───███████ │+
 * │                 │+
 * │       ─┴─       │
 * │                 │
 * └───┐         ┌───┘
 * │         │
 * │         │   + +
 * │         │
 * │         └──────────────┐
 * │                        │
 * │                        ├─┐
 * │                        ┌─┘
 * │                        │
 * └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
 * │ ─┤ ─┤       │ ─┤ ─┤
 * └──┴──┘       └──┴──┘  + + + +
 * 神兽保佑
 * 代码无BUG!
 */
@Service
public class TreeDepartmentServiceImpl implements TreeDepartmentService {

    @Resource
    private TreeDepartmentMapper treeDepartmentMapper;

    @Resource
    private DepartmentService departmentService;

    @Override
    public List<TreeDepartmentTypeInfo> getTreeDepartmentList(String tableName) {
        List<RelationLevel>  relationLevelList = null;
        if(ProjectConstants.EPC_DEPARTMENT.equals(tableName)){
            relationLevelList = treeDepartmentMapper.getTreeDepartmentList(tableName);
        }
        if (relationLevelList == null || relationLevelList.isEmpty()) {
            return new ArrayList<>();
        }

        List<Department> departmentList = departmentService.lambdaQuery().list();
        ArrayList<TreeDepartmentTypeInfo> treeDepartmentTypeInfos = new ArrayList<>();
        for (RelationLevel relationLevel : relationLevelList) {
            TreeDepartmentTypeInfo treeDepartmentTypeInfo = new TreeDepartmentTypeInfo();
            treeDepartmentTypeInfo.setId(relationLevel.getId());
            treeDepartmentTypeInfo.setDataId(relationLevel.getDataId());
            treeDepartmentTypeInfo.setDirectParentTypeId(relationLevel.getDirectParentTypeId());

            //获取设备类型名称
            List<Department> collect = departmentList.stream().filter(equipmentType -> equipmentType.getId().equals(relationLevel.getDataId())).collect(Collectors.toList());
//            treeDeviceTypeInfo.setName(getTypeName(relationLevel));
            if(collect != null && collect.size() > 0){
                treeDepartmentTypeInfo.setDepartmentName(collect.get(0).getDepartmentName());
            }
            treeDepartmentTypeInfos.add(treeDepartmentTypeInfo);
        }

        return new DefaultTreeBuildFactory<TreeDepartmentTypeInfo>().doTreeBuild(treeDepartmentTypeInfos);

    }
}
